package e.g.com.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class Filter implements javax.servlet.Filter {


	public void destroy() {
		// TODO Auto-generated method stub
		
	}


	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest) arg0;
		HttpServletResponse response=(HttpServletResponse) arg1;
		HttpSession session=request.getSession();
		System.out.println(request.getContextPath()+request.getRequestURI());
		System.out.println(session.getAttribute("istrue"));
		response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
		if(session.getAttribute("role")==null||!session.getAttribute("role").toString().equals("admin")){
			String str = "<script language='javascript'>alert('会话过期,请重新登录');"
                    + "window.top.location.href='"
                    + request.getContextPath()+"/index.jsp"
                    + "';</script>";
            
            try {
                PrintWriter writer = response.getWriter();
                writer.write(str);
                writer.flush();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
		}
		else
		{
		  arg2.doFilter(arg0, arg1);
		}
		
	}


	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
